package com.wipz.eoa.iphone.officesupplies;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wipz.eoa.iphone.BaseAction;
import com.wipz.eoa.iphone.IBaseService;
import com.wipz.eoa.iphone.businessProcess.AgencyCoreService;
import com.wipz.eoa.iphone.businessProcess.TaskTransitionService;
import com.wipz.eoa.iphone.util.Constants;

public class OfficeSuppliesBackApplyAction extends BaseAction {
	private IOfficeSuppliesBackApplyService officeSuppliesBackApplyService;
	private AgencyCoreService agencyCoreService;
	private TaskTransitionService taskTransitionService;
	private IBaseService baseService;
	private Map bxSubmit;

	public Map getBxSubmit() {
		return bxSubmit;
	}


	public void setBxSubmit(Map bxSubmit) {
		this.bxSubmit = bxSubmit;
	}
	


	public void setBaseService(IBaseService baseService) {
		this.baseService = baseService;
	}
	

	public void setTaskTransitionService(TaskTransitionService taskTransitionService) {
		this.taskTransitionService = taskTransitionService;
	}

	private List<Object> list;

	private Map view;

	public Map getView() {
		return view;
	}

	public void setView(Map view) {
		this.view = view;
	}

	public List<Object> getList() {
		return list;
	}

	public void setList(List<Object> list) {
		this.list = list;
	}
	
	public void setAgencyCoreService(AgencyCoreService agencyCoreService) {
		this.agencyCoreService = agencyCoreService;
	}
	
	public void setOfficeSuppliesBackApplyService(
			IOfficeSuppliesBackApplyService officeSuppliesBackApplyService) {
		this.officeSuppliesBackApplyService = officeSuppliesBackApplyService;
	}

	public String list() {
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		if (COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("COMMITERCODE", COMMITERCODE);
		params.put("BILLCODE", getRequest().getParameter("BILLCODE"));
		params.put("SUPLIESTYPE", getRequest().getParameter("SUPLIESTYPE"));
		params.put("PROCESS_STATUS", getRequest()
				.getParameter("PROCESS_STATUS"));
		params.put("APPLYDATE", getRequest().getParameter("APPLYDATE"));
		list = officeSuppliesBackApplyService.queryList(
				"officesuppliesBackApply.querylist", params);
		return SUCCESS;
	}

	/**
	 * 办公用品归还详细
	 * @return
	 * 2012-2-13 下午03:34:46
	 */
	public String view() {
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE");
		String ID = this.getRequest().getParameter("id");
		String taskId = this.getRequest().getParameter("taskId");
		if (COMMITERCODE.isEmpty() || ID.isEmpty()) {
			return SUCCESS;
		}
		view = (Map) baseService.queryOne("officesupplies_Backapply.queryByID", ID);
		
		Map newMap = (Map) baseService.queryOne("businessProcess.queryByCode",view);
		String processId = newMap == null ? "":(String)newMap.get("PROCESSID") ;
		view.put("processInstanceId", processId);
		String userId = (String) baseService.queryOne("user.queryId",COMMITERCODE);
		view = agencyCoreService.agencyList(Constants.OFFICESUPPLIESBACKAPPLY_BUSINESS_ID, userId,view);
		
		if(null == taskId || taskId.isEmpty()) {
			return SUCCESS;
		}
		
		view.put("taskId", taskId);
		List list =(taskTransitionService.getTaskTransition(taskId));
		Map url = new HashMap();
		StringBuffer bf = new StringBuffer();
		bf.append("/officeSupplies/OfficeSuppliesBackApplyAction_bxSubmit.action?id=");
		bf.append(ID);
		bf.append("&COMMITERCODE=");
		bf.append(COMMITERCODE);
		bf.append("&taskId=");
		bf.append(taskId);
		url.put("url", bf.toString());
		list.add(url);
		view.put("button", list);
		return SUCCESS;
	}
	
	/**
	 *  办公用品归还审批
	 * @return
	 * 2012-2-15 下午04:06:27
	 */
	public String bxSubmit() {
		//获取ID
		String id = this.getRequest().getParameter("id");
		//用户代号
		String COMMITERCODE = this.getRequest().getParameter("COMMITERCODE"); 
		if(id.isEmpty() || COMMITERCODE.isEmpty()) {
			return SUCCESS;
		}
		//任务唯一标识taskId
		String taskId =  this.getRequest().getParameter("taskId");
		//处理意见
		String commentContent =  this.getRequest().getParameter("comment");

		try {
			String s = new String(commentContent.getBytes("iso-8859-1"),"utf-8");
			commentContent = java.net.URLDecoder.decode(s, "utf-8");
		} catch (UnsupportedEncodingException e) {
			return SUCCESS;
		}
		
		//流向
		String transition = this.getRequest().getParameter("name");
		//处理类型
		String typeaccept = this.getRequest().getParameter("pass");
		Map map = new HashMap();
		map.put("ID", id);
		Map queryByIdMap = (Map) baseService.queryOne("officesupplies_Backapply.queryByID", map);
		//用品编号
		map.put("SUPLIESCODE", queryByIdMap.get("SUPLIESCODE"));
		Map processidMap = (Map) baseService.queryOne("businessProcess.queryByCode", queryByIdMap);
		//流程编号
		String PROCESSID = (String) processidMap.get("PROCESSID");
		map.put("PROCESSID", PROCESSID);
		map.put("COMMITERCODE", COMMITERCODE);
		map.put("taskId", taskId);
		map.put("commentContent",commentContent );
		map.put("transition", transition);
		map.put("typeaccept",typeaccept );
		Map mapUuid = (Map) baseService.queryOne("user.queryAll", COMMITERCODE);
		map.put("UuitId",mapUuid.get("ORGID"));
		
		bxSubmit = officeSuppliesBackApplyService.bxSubmit(map);
		return SUCCESS;
	}
}
